﻿using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Util
{
    /// <summary>
    /// 日志帮助类
    /// </summary>
    public static class LogHelper
    {
        /// <summary>
        /// 写入日志到本地TXT文件
        /// 注：日志文件名为"A_log.txt",目录为根目录
        /// </summary>
        /// <param name="log">日志内容</param>
        public static void WriteLog_LocalTxt(string log)
        {
            Task.Run(() =>
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A_log.txt");
                string logContent = $"{DateTime.Now.ToCstTime().ToString("yyyy-MM-dd HH:mm:ss")}:{log}\r\n";
                File.AppendAllText(filePath, logContent);
            });
        }

        /// <summary>
        /// 写sql操作日志
        /// </summary>
        /// <param name="log"></param>
        public static void WriteSqlLog_LocalTxt(SqlInfo log)
        {
            Task.Run(() =>
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log\\", DateTime.Now.ToCstTime().ToString("yyyy-MM-dd"));

                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string diskPath = filePath + "\\" + DateTime.Now.ToCstTime().ToString("HH") + ".txt";
                // filePath += "\\"+DateTime.Now.ToCstTime().ToString("HH")+":00.txt";
                StringBuilder logContent = new StringBuilder();
                logContent.Append($"操作时间:{DateTime.Now.ToCstTime().ToString("yyyy-MM-dd HH:mm:ss")}\r\n");
                logContent.Append($"操作人员:{log.UserId}\r\n");
                logContent.Append($"操作接口:{log.Route}\r\n");
                logContent.Append($"操作语句:{log.Sql}\r\n");
                if (!log.Remark.IsNullOrEmpty())
                {
                    logContent.Append($"备注说明:{log.Remark}\r\n");
                }
                logContent.Append($"-------------------------------\r\n");
                File.AppendAllText(diskPath, logContent.ToString());
            });
        }

        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="log"></param>
        public static void WriteErrLog_Redis(RedisInfo log)
        {
            Task.Run(() =>
            {
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ErrorLog\\", DateTime.Now.ToCstTime().ToString("yyyy-MM-dd"));

                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string diskPath = filePath + "\\" + DateTime.Now.ToCstTime().ToString("HH") + ".txt";
                // filePath += "\\"+DateTime.Now.ToCstTime().ToString("HH")+":00.txt";
                StringBuilder logContent = new StringBuilder();
                logContent.Append($"操作时间:{DateTime.Now.ToCstTime().ToString("yyyy-MM-dd HH:mm:ss")}\r\n");
                logContent.Append($"调用源头:{log.MethodName}\r\n");
                logContent.Append($"错误简述:{log.ErrorInfo}\r\n");
                logContent.Append($"错误详情:{log.Remark}\r\n");
                logContent.Append($"-------------------------------\r\n");
                File.AppendAllText(diskPath, logContent.ToString());
            });
        }
    }

    /// <summary>
    /// 传入的sql
    /// </summary>
    public class SqlInfo
    {
        /// <summary>
        /// 调用者ID
        /// </summary>
        public string UserId { get; set; }
        /// <summary>
        /// 调用接口路径
        /// </summary>
        public string Route { get; set; }
        /// <summary>
        /// sql语句
        /// </summary>
        public string Sql { get; set; }
        /// <summary>
        /// 备注说明
        /// </summary>
        public string Remark { get; set; }
    }

    /// <summary>
    /// redis错误日志实体
    /// </summary>
    public class RedisInfo
    {
        /// <summary>
        /// 来源
        /// </summary>
        public object MethodName { get; set; }
        /// <summary>
        /// 错误信息
        /// </summary>
        public String ErrorInfo { get; set; }
        /// <summary>
        /// 错误详情
        /// </summary>
        public String Remark { get; set; }
    }
}